/*
 * ioGame
 * Copyright (C) 2021 - 2023  渔民小镇 （262610965@qq.com、luoyizhu@gmail.com） . All Rights Reserved.
 * # iohao.com . 渔民小镇
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General  License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General  License for more details.
 *
 * You should have received a copy of the GNU General  License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.mygame.common.protobuf;

import com.mygame.common.kit.StrKit;
import com.thoughtworks.qdox.model.JavaClass;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 渔民小镇
 * @date 2022-01-24
 */

public class ProtoJava {
    public Class<?> clazz;
    String className;

    String comment;

    String fileName;
    String filePackage;

    JavaClass javaClass;

    List<ProtoJavaField> protoJavaFieldList = new ArrayList<>();

    public void addProtoJavaFiled(ProtoJavaField protoJavaField) {
        this.protoJavaFieldList.add(protoJavaField);
    }

    public boolean inThisFile(ProtoJava protoJava) {
        return Objects.equals(this.fileName, protoJava.fileName) && Objects.equals(this.filePackage, protoJava.filePackage);
    }

    public ProtoJavaRegionKey getProtoJavaRegionKey() {
        return new ProtoJavaRegionKey(this.fileName, this.filePackage);

    }

    public String toProtoMessage() {

        String fieldsString = protoJavaFieldList
                .stream()
                .map(ProtoJavaField::toProtoFieldLine)
                .collect(Collectors.joining("\n"));

        Map<String, String> messageMap = new HashMap<>();
        messageMap.put("className", this.className);
        messageMap.put("fieldsString", fieldsString);
        messageMap.put("classComment", this.comment);
        messageMap.put("classOrEnum", clazz.isEnum() ? "enum" : "message");

        String template = """
                // {classComment}
                {classOrEnum} {className} {
                {fieldsString}
                }
                                
                """;
        return StrKit.format(template, messageMap);
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public ProtoJava setClazz(Class<?> clazz) {
        this.clazz = clazz;
        return this;
    }

    public String getClassName() {
        return className;
    }

    public ProtoJava setClassName(String className) {
        this.className = className;
        return this;
    }

    public String getComment() {
        return comment;
    }

    public ProtoJava setComment(String comment) {
        this.comment = comment;
        return this;
    }

    public String getFileName() {
        return fileName;
    }

    public ProtoJava setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public String getFilePackage() {
        return filePackage;
    }

    public ProtoJava setFilePackage(String filePackage) {
        this.filePackage = filePackage;
        return this;
    }

    public JavaClass getJavaClass() {
        return javaClass;
    }

    public ProtoJava setJavaClass(JavaClass javaClass) {
        this.javaClass = javaClass;
        return this;
    }

    public List<ProtoJavaField> getProtoJavaFieldList() {
        return protoJavaFieldList;
    }

    public ProtoJava setProtoJavaFieldList(List<ProtoJavaField> protoJavaFieldList) {
        this.protoJavaFieldList = protoJavaFieldList;
        return this;
    }
}
